import { NMTopoBaseCommand } from '@app-cad/environments/nextmesh/types/command';
import { useState } from 'react';
import { TopoDialogProps } from '../type';
import { CommandDialog, pickedLineData } from '@amcax/base';

import { CommandFormWrapper } from '@app-cad/common/components/command-form';
import { PickInputForFocus } from '@app-cad/common/components/command-form/auto-focus/pick-input';
import { CommandFormFooter } from '@app-cad/common/components/command-form/footer';
import i18n from '@app-cad/common/utils/i18next';

import { Popover, Button } from 'antd';
import { DirectionGroup } from '../cmd-direction';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { documentV2 } from '@app-nextmesh/domain';
import { getNormalsOrIds } from '../cmd-common/tools';
import { PickInputValueType } from '@app-base/common/components/pick-input/types';

function Command({ title, onClose }: TopoDialogProps) {
  // const picker = PickerMgr.getPicker<Picker3D>('3d');
  const [sourceData, setSourceData] = useState<pickedLineData[]>([]);
  const [directionText, setButtonText] = useState('选择方向');
  const [directionVisible, setDirectionVisible] = useState(false);
  const [directionData, setDirectionData] = useState([]);
  useCommand('cmdCopy', {
    onClose,
  });

  const onSourcePickData = (sourceArr) => {
    setSourceData(sourceArr);
  };
  const isValid = () => {
    return sourceData.length > 0 && directionText !== '选择方向';
  };
  const handleSubmit = async () => {
    if (!isValid()) {
      return;
    }
    const ids = getNormalsOrIds(sourceData, 'id').map((item) => item.id);
    const params = {
      mode: 'copy',
      source: ids,
      relyOn: [directionData],
    };
    await documentV2.transform(params);
    setButtonText('选择方向');
    onClose();
  };

  const onDirectionData = (data) => {
    setDirectionVisible(data.directionVisible);
    setButtonText(data.directionText);
    setDirectionData(data.directionVec);
  };
  return (
    <CommandDialog
      title={title}
      placement='topLeft'
      onClose={onClose}>
      <CommandFormWrapper>
        <PickInputForFocus
          autoFocus
          controlled
          allowPickType={[
            PickInputValueType.Vertex,
            PickInputValueType.Curve,
            PickInputValueType.Surface,
          ]}
          onPick={onSourcePickData}
          value={sourceData}
          placeholder='请选择点/线/面'
        />
        <Popover
          getPopupContainer={() => document.querySelector('#app-cad_root')}
          trigger='click'
          title='选择方向'
          open={directionVisible}
          content={<DirectionGroup onDirectionSubmit={onDirectionData} />}>
          <Button onClick={() => setDirectionVisible(true)}>
            {directionText}
          </Button>
        </Popover>
        <CommandFormFooter
          onClose={onClose}
          onSubmit={handleSubmit}
          submitText='完成'
          isSubmitDisabled={!isValid()}
        />
      </CommandFormWrapper>
    </CommandDialog>
  );
}

export const cmdTopoCopy: NMTopoBaseCommand = {
  id: 'cmdTopoCopy',
  label: 'command.topo.copy',
  icon: 'commands/feature/copy',
  create: function (label: string, onClose: () => void) {
    return (
      <Command
        title={i18n.t(label)}
        onClose={onClose}
      />
    );
  },
};
